package com.occludens.server.dispatch;

import java.lang.reflect.ParameterizedType;
import java.util.logging.Logger;

import com.google.code.twig.ObjectDatastore;
import com.google.inject.Provider;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.Action;
import com.gwtplatform.dispatch.shared.ActionException;
import com.gwtplatform.dispatch.shared.Result;
import com.occludens.shared.model.UserInfo;

public abstract class DispatchActionHandler <A extends Action<R>, R extends Result>
		implements ActionHandler<A, R> {

	private final Class<A> actionClass;

	protected final Provider<UserInfo> userInfoProvider;
	//protected final ObjectDatastore datastore;

	protected static final Logger logger = Logger
			.getLogger(DispatchActionHandler.class.getName());

	@SuppressWarnings("unchecked")
	//public DispatchActionHandler(final Provider<UserInfo> userInfoProvider,	final ObjectDatastore datastore) {
	public DispatchActionHandler(final Provider<UserInfo> userInfoProvider) {
		// this works because DispatchActionHandler is abstract. See
		// http://www.bewareofthebear.com/java/instantiating-generic-type-parameters/
		this.actionClass = (Class<A>) ((ParameterizedType) this.getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
		this.userInfoProvider = userInfoProvider;
		//this.datastore = datastore;

	}

	@Override
	public Class<A> getActionType() {
		return actionClass;
	}

	@Override
	public void undo(A action, R result, ExecutionContext context)
			throws ActionException {
		// by default not undoable
	}

}
